﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class LogTranslation
    {
        #region Properties
        public Language Language
        {
            get
            {
                return this.GetLanguage();
            }
        }

        public User User
        {
            get
            {
                return this.GetUser();
            }
        }

        public Object Object
        {
            get
            {
                return this.GetObjectKind();
            }
        }

        public Action Action
        {
            get
            {
                return this.GetAction();
            }
        }
        #endregion

        #region Methods
        public Language GetLanguage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Languages.Where(p => p.ID == this.ID_Language);

            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public User GetUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_User);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Object GetObjectKind()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Objects.Where(p => p.ID == this.ObjectKind);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Action GetAction()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Actions.Where(p => p.ID == this.ID_Action);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<ErrorItem> Rollback(int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = 0;
            try
            {
                switch (this.ObjectKind)
                {
                    //Image
                    case 1:
                        {
                            LogImage log = _db.LogImages.Single(p=>p.ID == this.ID_ChangeLog);
                            errorList = log.Rollback(id_User, id_Lang);
                            break;
                        }
                    //Category
                    case 2:
                        {
                            LogCategory log = _db.LogCategories.Single(p => p.ID == this.ID_ChangeLog);
                            errorList = log.Rollback(id_User, id_Lang);
                            break;
                        }
                    //Customer
                    case 3:
                        {
                            LogCustomer log = _db.LogCustomers.Single(p => p.ID == this.ID_ChangeLog);
                            errorList = log.Rollback(id_User, id_Lang);
                            break;
                        }
                    //Info
                    case 4:
                        {

                            LogInfo log = _db.LogInfos.Single(p => p.ID == this.ID_ChangeLog);
                            errorList = log.Rollback(id_User, id_Lang);
                            break;
                        }
                    //Email
                    case 5:
                        {

                            LogEmail log = _db.LogEmails.Single(p => p.ID == this.ID_ChangeLog);
                            errorList = log.Rollback(id_User, id_Lang);
                            break;
                        }
                    //User
                    case 6:
                        {
                            LogUser log = _db.LogUsers.Single(p => p.ID == this.ID_ChangeLog);
                            errorList = log.Rollback(id_User, id_Lang);
                            break;
                        }
                }
            }
            catch
            {
                errorMess = 48;
            }

            if (errorMess != 0)
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Static Methods
        public static System.Object GetTranslatedObjectList(int objectKind)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            System.Object list = null;

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        list = (from obs in _db.Images
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Category
                case 2:
                    {
                        list = (from obs in _db.Categories
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Customer
                case 3:
                    {
                        list = (from obs in _db.Customers
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Info
                case 4:
                    {
                        list = (from obs in _db.Infos
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Email
                case 5:
                    {
                        list = (from obs in _db.Emails
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID_Email)
                                select obs).OrderByDescending(p=>p.CreatedTime).ToList();
                        break;
                    }
                //User
                case 6:
                    {
                        list = (from obs in _db.Users
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
            }

            return list;
        }

        public static System.Object GetTranslatedObjectList(int objectKind, int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            System.Object list = null;

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        list = (from obs in _db.Images
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Category
                case 2:
                    {
                        list = (from obs in _db.Categories
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Customer
                case 3:
                    {
                        list = (from obs in _db.Customers
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Info
                case 4:
                    {
                        list = (from obs in _db.Infos
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Email
                case 5:
                    {
                        list = (from obs in _db.Emails
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID_Email)
                                select obs).OrderByDescending(p=>p.CreatedTime).ToList();
                        break;
                    }
                //User
                case 6:
                    {
                        list = (from obs in _db.Users
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
            }

            return list;
        }

        public static System.Object GetTranslatedObjectList(int objectKind, DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            System.Object list = null;

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        list = (from obs in _db.Images
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Category
                case 2:
                    {
                        list = (from obs in _db.Categories
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Customer
                case 3:
                    {
                        list = (from obs in _db.Customers
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Info
                case 4:
                    {
                        list = (from obs in _db.Infos
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Email
                case 5:
                    {
                        list = (from obs in _db.Emails
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID_Email)
                                select obs).OrderByDescending(p=>p.CreatedTime).ToList();
                        break;
                    }
                //User
                case 6:
                    {
                        list = (from obs in _db.Users
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
            }

            return list;
        }

        public static System.Object GetTranslatedObjectList(int objectKind, DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            System.Object list = null;

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        list = (from obs in _db.Images
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Category
                case 2:
                    {
                        list = (from obs in _db.Categories
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Customer
                case 3:
                    {
                        list = (from obs in _db.Customers
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Info
                case 4:
                    {
                        list = (from obs in _db.Infos
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Email
                case 5:
                    {
                        list = (from obs in _db.Emails
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID_Email)
                                select obs).OrderByDescending(p=>p.CreatedTime).ToList();
                        break;
                    }
                //User
                case 6:
                    {
                        list = (from obs in _db.Users
                                where (from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
            }

            return list;
        }

        public static System.Object GetNonTranslatedObjectList(int objectKind)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            System.Object list = null;

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        list = (from obs in _db.Images
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Category
                case 2:
                    {
                        list = (from obs in _db.Categories
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Customer
                case 3:
                    {
                        list = (from obs in _db.Customers
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Info
                case 4:
                    {
                        list = (from obs in _db.Infos
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Email
                case 5:
                    {
                        list = (from obs in _db.Emails
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID_Email)
                                select obs).OrderByDescending(p=>p.CreatedTime).ToList();
                        break;
                    }
                //User
                case 6:
                    {
                        list = (from obs in _db.Users
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
            }

            return list;
        }

        public static System.Object GetNonTranslatedObjectList(int objectKind, int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            System.Object list = null;

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        list = (from obs in _db.Images
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Category
                case 2:
                    {
                        list = (from obs in _db.Categories
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Customer
                case 3:
                    {
                        list = (from obs in _db.Customers
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Info
                case 4:
                    {
                        list = (from obs in _db.Infos
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Email
                case 5:
                    {
                        list = (from obs in _db.Emails
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID_Email)
                                select obs).OrderByDescending(p=>p.CreatedTime).ToList();
                        break;
                    }
                //User
                case 6:
                    {
                        list = (from obs in _db.Users
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Month == month && logs.ActionTime.Year == year select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
            }

            return list;
        }

        public static System.Object GetNonTranslatedObjectList(int objectKind, DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            System.Object list = null;

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        list = (from obs in _db.Images
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Category
                case 2:
                    {
                        list = (from obs in _db.Categories
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Customer
                case 3:
                    {
                        list = (from obs in _db.Customers
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Info
                case 4:
                    {
                        list = (from obs in _db.Infos
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Email
                case 5:
                    {
                        list = (from obs in _db.Emails
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID_Email)
                                select obs).OrderByDescending(p=>p.CreatedTime).ToList();
                        break;
                    }
                //User
                case 6:
                    {
                        list = (from obs in _db.Users
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date == date.Date select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
            }

            return list;
        }

        public static System.Object GetNonTranslatedObjectList(int objectKind, DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            System.Object list = null;

            switch (objectKind)
            {
                //Image
                case 1:
                    {
                        list = (from obs in _db.Images
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Category
                case 2:
                    {
                        list = (from obs in _db.Categories
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Customer
                case 3:
                    {
                        list = (from obs in _db.Customers
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Info
                case 4:
                    {
                        list = (from obs in _db.Infos
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
                //Email
                case 5:
                    {
                        list = (from obs in _db.Emails
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID_Email)
                                select obs).OrderByDescending(p=>p.CreatedTime).ToList();
                        break;
                    }
                //User
                case 6:
                    {
                        list = (from obs in _db.Users
                                where !(from logs in _db.LogTranslations where logs.ObjectKind == objectKind && logs.ActionTime.Date >= beginDate && logs.ActionTime.Date <= endDate select logs.ID_Object).Contains(obs.ID)
                                select obs).OrderByDescending(p=>p.ID).ToList();
                        break;
                    }
            }

            return list;
        }

        public static List<LogTranslation> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            return _db.LogTranslations.OrderByDescending(p=>p.ActionTime).ToList();
        }

        public static List<ErrorItem> Insert(int id_User, int id_LangTrans, int id_Object, int objectKind, int id_ChangeLog, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.AddTranslationActivity(id_User, id_LangTrans, id_Object, objectKind, id_ChangeLog);
            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Search
        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (query.Length != 0)
                    query += " AND ";
                if (kvp.Key == "ID_User" || kvp.Key == "ID_Language" || kvp.Key == "ID_Object" || kvp.Key == "ObjectKind" || kvp.Key == "ID_Action")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "ActionTime")
                {
                    if (DateTime.Parse(kvp.Value.ToString()) != noValue)
                        query += kvp.Key + " = #" + kvp.Value.ToString() + "#";
                }
            }

            return query;
        }

        public static System.Object GetLogTranslation(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);

            if (oneRow)
            {
                LogTranslation i = _db.ExecuteQuery<LogTranslation>(query).First();
                return i;
            }
            List<LogTranslation> iList = _db.ExecuteQuery<LogTranslation>(query).OrderByDescending(p => p.ActionTime).ToList();
            return iList;
        }
        #endregion
    }
}